/**
 * Created by XianTao on 2016/7/15
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.smartspring.data.dictionary.restcontroller;

import lombok.extern.slf4j.Slf4j;
import org.smartspring.data.dictionary.entity.Dictionary;
import org.smartspring.data.dictionary.service.DictionaryService;
import org.smartspring.jpa.base.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/dictionary", method = RequestMethod.POST)
@Slf4j
public class DictionaryRestController {
    @Autowired
    private DictionaryService dictionaryService;

    @RequestMapping(value = "/listByPage")
    public ResultVo<Page<Dictionary>> listByPage() {
        Page<Dictionary> dictionaryPage = dictionaryService.findAll(null, new PageRequest(1, 10));
        return new ResultVo<Page<Dictionary>>(ResultVo.STATUS_SUCCESS, "", dictionaryPage);
    }

    @RequestMapping(value = "/listOne/{id}")
    public ResultVo<Dictionary> listOne(@PathVariable("id") String id) {
        Dictionary dictionary = dictionaryService.getOne(id);
        return new ResultVo<Dictionary>(ResultVo.STATUS_SUCCESS, "", dictionary);
    }

    @RequestMapping(value = "/save")
    public ResultVo<Dictionary> save(@RequestBody final Dictionary dictionary) {
        Dictionary resultDictionary = dictionaryService.saveAndFlush(dictionary);
        return new ResultVo<Dictionary>(ResultVo.STATUS_SUCCESS, "", resultDictionary);
    }
}
